//
// Created by xili on 2024/7/30 15:48.
// Go big or go home.
//
#include <vector>
#include <climits>
#include <algorithm>

using namespace std;

class Solution {
public:
    int minimumDifference(vector<int> &nums, int k) {
        int diff = INT_MAX;
        sort(nums.begin(), nums.end());
        int r = 0, l = 0;
        while (r < nums.size()) {
            r++;
            if (r - l == k) {
                diff = min(diff, (nums[r - 1] - nums[l]));
                l++;
            }
        }
        return diff;
    }
};